﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Caching;
using StripeOne.Web.StripeOneServiceReference;

namespace StripeOne.Core
{
    public abstract class StripeOneController : Controller
    {
        public StripeOneController() { }

        protected abstract bool RequireAuthentication { get; }

        private User _user = null;
        protected new User User
        {
            get
            {
                if (HttpContext.Session["user"] == null)
                    this._user = new User();

                return this._user;
            }
            set
            {
                HttpContext.Cache.Add("user", value, new CacheDependency("WEB_USER"), DateTime.Now.AddDays(7), new TimeSpan(7, 0, 0, 0), CacheItemPriority.Default, null);
                this._user = value;
            }
        }
        
        protected bool IsAjaxRequest()
        {
            return HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest";
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            string currentUrl = filterContext.HttpContext.Request.Url.ToString();

            if (this.RequireAuthentication)
                filterContext.Result = Redirect("~/Login/Index?refUrl=" + currentUrl);
        }
    }
}